package com.generator.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.generator.controller.vo.ColumnDefinitionVo;
import com.generator.entity.DatasourceConfig;
import com.generator.gen.*;
import com.generator.service.DatasourceConfigService;
import com.generator.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author tanghc
 */
@RestController
@RequestMapping("/generator/datasourceconfig")
public class DatasourceConfigController {

    @Autowired
    private DatasourceConfigService datasourceConfigService;


    @RequestMapping("/list")
    public ResponseEntity list(Page pageDto, DatasourceConfig datasourceConfig) {
        return datasourceConfigService.list(pageDto,datasourceConfig);
    }

    @RequestMapping("/save")
    public ResponseEntity update(@RequestBody DatasourceConfig datasourceConfig) {
        return datasourceConfigService.save(datasourceConfig);
    }

    @RequestMapping("/del")
    public ResponseEntity del(@RequestBody List<Integer> ids) {
        datasourceConfigService.del(ids);
        return ResponseEntity.ok().build();
    }

    @RequestMapping("/table/{id}")
    public ResponseEntity listTable(@PathVariable("id") int id,Page pageDto,TableDefinition tableDefinition) {
        DatasourceConfig dataSourceConfig = datasourceConfigService.getById(id);
        GeneratorConfig generatorConfig = GeneratorConfig.build(dataSourceConfig);
        SQLService service = SQLServiceFactory.build(generatorConfig);
        Page simpleTableDefinitionsPage = service.getTableSelector(generatorConfig).getSimpleTableDefinitionsPage(pageDto, tableDefinition);
        return ResponseEntity.ok(simpleTableDefinitionsPage);
    }

    @RequestMapping("/column/{id}/{table}")
    public ResponseEntity listcolumn(@PathVariable("id") int id,@PathVariable("table") String table) {
        DatasourceConfig dataSourceConfig = datasourceConfigService.getById(id);
        GeneratorConfig generatorConfig = GeneratorConfig.build(dataSourceConfig);
        SQLService service = SQLServiceFactory.build(generatorConfig);
        List<ColumnDefinition> columnDefinitions = service.getTableSelector(generatorConfig).getColumnSelector().getColumnDefinitions(table);
        return ResponseEntity.ok( CopyUtil.copyList(columnDefinitions, ColumnDefinitionVo.class));

    }

    @RequestMapping("/test")
    public ResponseEntity test(@RequestBody DatasourceConfig datasourceConfig) {
        String error = DBConnect.testConnection(GeneratorConfig.build(datasourceConfig));
        if (error != null) {
            return ResponseEntity.internalServerError().body(error);
        }
        return ResponseEntity.ok().build();
    }

    @RequestMapping("/dbtype")
    public ResponseEntity dbType(@RequestBody DatasourceConfig datasourceConfig) {
        List<DbTypeShow> dbTypeShowList = Stream.of(DbType.values())
                .map(dbType -> new DbTypeShow(dbType.getDisplayName(), dbType.getType()))
                .collect(Collectors.toList());
        return ResponseEntity.ok(dbTypeShowList);
    }

    private static class DbTypeShow {
        private String label;
        private Integer dbType;

        public DbTypeShow(String label, Integer dbType) {
            this.label = label;
            this.dbType = dbType;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public Integer getDbType() {
            return dbType;
        }

        public void setDbType(Integer dbType) {
            this.dbType = dbType;
        }
    }


}
